from GBDTModel import GBDTModel
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import DataFrame
sns.set()


# 处理新数据

def main():
    dataset = pd.read_excel('../data/newData.xlsx')
    # 要分析的数据
    X = dataset.iloc[5:, 1:360].values

    print("======原始数据的形状========")
    print(np.array(X).shape)
    print(X)

    # 转置
    X_index = [[row[i] for row in X] for i in range(len(X[0]))]
    print(np.array(X_index).shape)
    second_data_predict = get_three_layer_high(X_index)
    print(int(len(second_data_predict) / 4))
    print("second_data_predict:%s" % (second_data_predict))
    # 预测值处理
    predict = []
    for i in range(int(len(second_data_predict) / 4)):
        tmp = second_data_predict[4 * i:4 * i + 4]
        # print(tmp)
        max = 0
        for j in range(4):
            if tmp[j] > max:
                max = tmp[j]
        predict.append(max)

    print("predict:%s" % (predict))
    print(np.array(predict).shape)

    df = DataFrame(predict)
    df.to_excel('../excelData/new_predict.xlsx')

    # 原始机测、手测数据
    mmv = pd.read_excel('../excelData/machine_manual_values.xlsx')
    print("======原始机测、手测数据的形状========")
    print(np.array(mmv).shape)
    # print(mmv)
    first_machine_value = mmv.iloc[1, 1:34].values
    first_manual_value = mmv.iloc[0, 1:34].values
    second_machine_value = mmv.iloc[4, 1:28].values
    second_manual_value = mmv.iloc[3, 1:28].values

    # second_machine_value = [[second_machine_value[i] for row in second_machine_value] for i in
    #                        range(len(second_machine_value))]
    print(np.array(second_machine_value).shape)
    print(second_machine_value)
    # 水平线
    leavel = [0] * 27
    # 机测与手测误差
    machine_bias = abs(second_machine_value - second_manual_value)
    # 预测与手测误差
    predict_bias = abs(predict - second_manual_value)
    print("predict_bias:%s" % (predict_bias))
    print("machine_bias:%s" % (machine_bias))
    # 预测、机测、手测 画图
    # 解决中文显示问题
    plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
    plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
    plt.figure(figsize=(15, 15))
    # 图一：每年上映电影的总收入
    ax = plt.subplot(111)
    # 设置x轴 范围
    ax.set_xlim(0, 30)
    # 设置x轴 主刻度，（次刻度设置minor=True）
    ax.set_xticks(np.arange(0, 30, 1), minor=False)
    # 画图
    xx = np.linspace(1, 27, 27)
    ax.plot(xx, machine_bias, linestyle='--', marker='o', markersize=5, color='green')
    ax.plot(xx, leavel, linestyle='--', marker='o', markersize=5, color='#900302')
    ax.plot(xx, predict_bias, linestyle='--', marker='o', markersize=5, color='#000000')
    plt.legend(('machine_bias', 'leavel','predict_bias'), loc='upper right')
    # 增加竖线
    # ax.axvline(x=y_manual[i], color='#d46061', linewidth=1)
    # ax.axvline(x=k_manual[i], color='#000000', linewidth=1)
    # ax.axvline(x=q_manual[i], color='#F9F900', linewidth=1)
    ax.set_title("空气层和清液层分界面机测与本模型预测绝对值误差")
    ax.set_ylabel("高度误差")
    f = plt.gcf()  # 获取当前图像
    plt.show()
    f.savefig('../data/predict_mechine_contrast_abs.png')

    print(np.array(first_machine_value).shape)
    print(first_machine_value)
    print(np.array(first_manual_value).shape)
    print(first_manual_value)
    print(np.array(second_machine_value).shape)
    print(second_machine_value)
    print(np.array(second_manual_value).shape)
    print(second_manual_value)

    # # 将第三层高度写入数组
    # # df = DataFrame(data)
    # df.to_excel('../excelData/three_layer_high.xlsx')

    # 获取第三层高度
    # input: 360*n  n组数据
    # return  n组数据对应的第三层高度


def get_three_layer_high(input):
    # 模型
    model = GBDTModel

    print("==========X_index=============")
    print(np.array(input).shape[0])
    three_layer_high_num = np.array(range(np.array(input).shape[0]), dtype=float)
    for i in range(np.array(input).shape[0]):
        # 处理空值
        for j in range(360):
            if np.isnan(input[i][j]):
                input[i][j] = 0
        res = model.predict(model, input[i], name)
        three_layer_high = (360 - res) * 0.05 + 0.6
        three_layer_high_num[i] = three_layer_high

    print("==========three_layer_high_num=============")
    print(np.array(three_layer_high_num).shape)
    # print(three_layer_high_num)
    return three_layer_high_num


name = "../data/GBDT"
num_of_index = 1
if __name__ == "__main__":
    main()
